package com.szholly.pro.watergas_server.ps.controller;


import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.data.DbManager;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.StationOnlineCompare;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.CompanyInfoService;
import com.szholly.pro.watergas_server.ps.service.MaterialReplaceService;
import com.szholly.pro.watergas_server.ps.service.RemoteService;
import com.szholly.pro.watergas_server.ps.service.StationInfoService;
import com.itextpdf.text.DocumentException;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

import freemarker.template.TemplateException;
import lrq.common.ExecptionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/StationInfo/*")
public class StationInfoController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	RemoteService remoteService;	
	@Autowired
	StationInfoService stationInfoService;
	@Autowired
	CompanyInfoService companyInfoService;	
	
	@Autowired
	MaterialReplaceService materialReplaceService;
	
	/*
	 * 查询本专题站点列表
	 */
	@RequestMapping(value = "/queryStationList")
	public void queryStationList(HttpServletRequest resq, HttpServletResponse res) throws Exception {		
		Map<String,String> params = queryStationQueryParams();
		Map<String,Object> result=stationInfoService.queryStationStatusCountList(params,null);
		JSONObject json = JSONObject.fromObject(result);
		json.put("pageInfo", JSONObject.fromObject(JsonUtil.getConfig()));
		outString(json.toString());
	}
	
	/**
	 * 站点列表，只为了check选择
	 * gislin
	 * 2016年8月15日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryStationListForSelect")
	public void queryStationListForSelect(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = queryStationQueryParams();
		List<RowBase> result=stationInfoService.queryStationList(params,null,null);		
		JSONArray json = JSONArray.fromObject(result);
		outString(json.toString());
	}
	/**
	 * 查询贡献断面站点
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="queryContributeStation")
	public void queryContributeStationList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = queryStationQueryParams();
		HashMap<String, Object> stationResult = new HashMap<String, Object>();
		List<RowBase> contriResult = stationInfoService.queryContributeStationList(params,null,null);
		List<RowBase> sewageResult = stationInfoService.querySewageStationList(params,null,null);
		stationResult.put("contribute",contriResult);
		stationResult.put("sewageResult",sewageResult);
		JSONObject json = JSONObject.fromObject(stationResult);
		outString(json.toString());
	}
	/**
	 * 传输稳定性统计
	 * gislin
	 * 2016年5月7日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/StationTransmissionStability")
	public void AllStationRealTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
	
		HashMap<String,String> params=new HashMap<>();		
		HashMap<String,String> queryobject=RequestUnit.getHashMapParmS();
		String station_id=(String) queryobject.get("queryObject[STATION_ID]");
		String startDate=(String) queryobject.get("objdata[startDate]");
		String endDate=(String) queryobject.get("objdata[endDate]");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		
		long day=0;
		//计算天数
		if(StringUtil.IsNotNullOrEmpty(startDate)&&StringUtil.IsNotNullOrEmpty(endDate)){
			//获得时间查询的总描述
			long miao=format.parse(endDate).getTime()-format.parse(startDate).getTime();
			day=miao/1000/60/60/24;//总秒数/毫秒/秒/分钟/小时=天数
		}
		//统计条件
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}	
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		//如果day=0，视为按天查询
		if(day==0){
			day=1;
		}
		HashMap<String,Object> resultlist=filtration(params,day);
		JSONObject result = JSONObject.fromObject(resultlist);
		outString(result.toString());
	}
	/**
	 * 传输稳定性导出
	 * @throws Exception 
	 */
	@RequestMapping(value="Fixedpoint")
	public void TransmissionStability() throws Exception{
		HashMap<String,String> params=new HashMap<>();		
		HashMap<String,String> queryobject=RequestUnit.getHashMapParmS();
		String station_id=(String) queryobject.get("queryObject[STATION_ID]");
		String startDate=(String) queryobject.get("objdata[startDate]");
		String endDate=(String) queryobject.get("objdata[endDate]");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		
		long day=0;
		//计算天数
		if(StringUtil.IsNotNullOrEmpty(startDate)&&StringUtil.IsNotNullOrEmpty(endDate)){
			//获得时间查询的总描述
			long miao=format.parse(endDate).getTime()-format.parse(startDate).getTime();
			day=miao/1000/60/60/24;//总秒数/毫秒/秒/分钟/小时=天数
		}
		//统计条件
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}	
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		//如果day=0，视为按天查询
		if(day==0){
			day=1;
		}
		HashMap<String,Object> resultlist=filtration(params,day);
		
		outWord(resultlist,"TransmissionStability.xml","传输稳定性统计.doc");
	}

	/**
	 * 传输稳定性统计导出PDF文档
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="TransmissionStabilityExportPDF")
	public void StationStatusexportPDF(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		HashMap<String,Object> queryobject=RequestUnit.getHashMapParmO();
		String startDate=(String) queryobject.get("startDate");
		String endDate=(String) queryobject.get("endDate");
		String dataType=RequestUnit.getString("dataType");
		String selectQuarter=RequestUnit.getString("selectQuarter");
		String station_id=(String) queryobject.get("STATION_DI");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		
		long day=0;
		//计算天数
		if(StringUtil.IsNotNullOrEmpty(startDate)&&StringUtil.IsNotNullOrEmpty(endDate)){
			//总秒数
			long miao=format.parse(endDate).getTime()-format.parse(startDate).getTime();
			day=miao/1000/60/60/24;//总秒数/毫秒/秒/分钟/小时=天数
		}
		HashMap<String,String> parms=new HashMap<>();
		HashMap<String, Object> data = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			parms.put("start",startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			parms.put("stationid",station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			parms.put("end",endDate);
		}
		//如果day=0，视为按天查询
		if(day==0){
			day=1;
		}
		HashMap<String,Object> resultlist=filtration(parms,day);
		data.put("StatusList", resultlist);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		data.put("dataType", dataType);
		data.put("selectQuarter", selectQuarter);
		outPDF(data, "TransmissionStability.html", "传输稳定性统计.pdf",false);
	}
	/**
	 * 当前站点状态统计
	 * @param resq
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping(value="StaictisStatus")
	public void StatictisStatus(HttpServletRequest resq, HttpServletResponse res) throws Exception{
				
		String station_id=RequestUnit.getString("STATION_ID");
		Map<String,String> params=new HashMap<>();
		
		String WATER_TYPE=RequestUnit.getString("WATER_TYPE");
		
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(WATER_TYPE)){
			params.put("water_type", WATER_TYPE);
		}
		Map<String,Object> stationList=StationCurrentData(params);				
		String result=JsonUtil.ConvertObjectToJson(stationList);
		outString(result);
	}
	/**
	 * 当前站点状态导出Pdf
	 * @param resq
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping(value="StationStatusExportPDF")
	public void StationStatusExportPDF(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_id=RequestUnit.getString("STATION_ID");
		Map<String,String> params=new HashMap<>();		
		String WATER_TYPE=RequestUnit.getString("WATER_TYPE");		
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(WATER_TYPE)){
			params.put("water_type", WATER_TYPE);
		}
		Map<String,Object> stationList=StationCurrentData(params);	
		outPDF(stationList, "stationStatus.html", "站点当前状态统计.pdf",true);
	}
	/**
	 * 获取站点当前状态信息
	 * @return
	 * @throws Exception 
	 */
	public Map<String,Object> StationCurrentData(Map<String,String> params) throws Exception{
		Map<String,Object> data=new HashMap<>();
		NumberFormat nt = NumberFormat.getPercentInstance();
		nt.setMinimumFractionDigits(2);//百分比保留小数点后两位数
		Map<String,Object> stationList=stationInfoService.StatictisStatus(params,null);		
		Double staFault=Double.valueOf(stationList.get("staFault").toString());
		Double staOverproof=Double.valueOf(stationList.get("staOverproof").toString());
		Double staNormal=Double.valueOf(stationList.get("staNormal").toString());
		Double staExecption=Double.valueOf(stationList.get("staExecption").toString());
		Double dataSize=Double.valueOf(stationList.get("dataSize").toString());
		data.put("staFaultRate", nt.format(staFault/dataSize));
		data.put("staOverproofRate", nt.format(staOverproof/dataSize));
		data.put("staNormalRate", nt.format(staNormal/dataSize));
		data.put("staExecptionRate", nt.format(staExecption/dataSize));
		data.put("stationList", stationList);
		return data;
	}
	/**
	 * 站点故障统计
	 * @param resq
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping(value="StatistisOperationAlarmProcess")
	public void OperationAlarmProcess(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> rowProcess=stationInfoService.queryOperationAlarmProcess(params);//获取站点故障项
		List<RowBase> rowtafault=stationInfoService.queryStationStafault(params);//获取故障站点
		data.put("rowProcess", rowProcess);
		data.put("rowtafault", rowtafault);
		String result=JsonUtil.ConvertObjectToJson(data);
		outString(result);
	}
	/**
	 * 站点仪器故障统计
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value="OperationAlarmProcessCos")
	public void OperationAlarmProcessCos(HttpServletRequest resq, HttpServletResponse res){
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> rowProcess=materialReplaceService.queryOperationAlarmProcess(params);//获取站点故障项
		List<RowBase> listEquipment=materialReplaceService.StatisticsFaultEquipment(params,null);//获取故障仪器
		data.put("rowProcess", rowProcess);
		data.put("rowtafault", listEquipment);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		String result=JsonUtil.ConvertObjectToJson(data);
		outString(result);
	}
	/**
	 * 故障汇总
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value="FaultStatics")
	public void FaultStatics(HttpServletRequest resq, HttpServletResponse res){
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> listFault=materialReplaceService.queryFaultStatics(params);//故障统计
		data.put("rowtafault",listFault);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		String result=JsonUtil.ConvertObjectToJson(data);
		outString(result);
	}
	/**
	 * 故障仪器导出PDF文档
	 * @param resq
	 * @param res
	 * @throws IOException 
	 * @throws TemplateException 
	 * @throws DocumentException 
	 */
	@RequestMapping(value="faultCosExportPDF")
	public void faultCosExportPDF(HttpServletRequest resq, HttpServletResponse res) throws IOException, DocumentException, TemplateException{
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> rowProcess=materialReplaceService.queryOperationAlarmProcess(params);//获取站点故障项
		List<RowBase> listEquipment=materialReplaceService.StatisticsFaultEquipment(params,null);//获取故障仪器
		data.put("rowProcess", rowProcess);
		data.put("rowtafault", listEquipment);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		outPDF(data, "faultStatisticsCos.html", "仪器故障统计.pdf",false);
	}	
	/**
	 * 站点故障导出PDF文档
	 * @param resq
	 * @param res
	 * @throws IOException 
	 * @throws TemplateException 
	 * @throws DocumentException 
	 */
	@RequestMapping(value="faultExportPdf")
	public void OperationAlarmProcessexportWord(HttpServletRequest resq, HttpServletResponse res) throws IOException, DocumentException, TemplateException{
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> rowProcess=stationInfoService.queryOperationAlarmProcess(params);//获取站点故障项
		List<RowBase> rowtafault=stationInfoService.queryStationStafault(params);//获取故障站点
		data.put("rowProcess", rowProcess);
		data.put("rowtafault", rowtafault);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		outPDF(data, "faultStatistics.html", "站点故障统计.pdf",false);
	}
	/**
	 * 导出障碍汇总pdf
	 * @param resq
	 * @param res
	 * @throws IOException
	 * @throws DocumentException
	 * @throws TemplateException
	 */
	@RequestMapping(value="faultStatisticsExportPdf")
	public void faultStatisticsExportPdf(HttpServletRequest resq, HttpServletResponse res) throws IOException, DocumentException, TemplateException{
		String station_id=RequestUnit.getString("STATION_ID");
		String startDate=RequestUnit.getString("start");
		String endDate=RequestUnit.getString("end");
		HashMap<String,String> params=new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("start", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("end", endDate);
		}
		
		HashMap<String, Object> data = new HashMap<>();
		List<RowBase> listFault=materialReplaceService.queryFaultStatics(params);//故障统计
		data.put("rowtafault",listFault);
		data.put("startDate", startDate);
		data.put("endDate", endDate);
		outPDF(data, "faultStatisticsAll.html", "故障汇总.pdf",false);
	}
	/**
	 * 时间划算 把分钟划算成天-小时-分钟
	 * @param time
	 * @return
	 */
	public String toTime(int time){
		int day=time/(60*24);//天
		int hours=time/60-day*24;//小时
		int min=time-hours*60-day*24*60;//分钟
		String datetime="";
		if(day>0){
			datetime+=day+"天";
		}
		if(hours>0){
			datetime+=hours+"小时";
		}
		if(min>0){
			datetime+=min+"分钟";
		}
		return datetime;
	}
	/**
	 * 获取状态统计信息
	 * @param params
	 * @param day
	 * @return
	 * @throws Exception
	 */
	public HashMap<String,Object> filtration(HashMap<String,String> params,long day) throws Exception{
		List<RowBase> list=stationInfoService.StatisticsStationInfo(params);
		List<Object> resultlist=new ArrayList<>();
		HashMap<String,Object> dataTotal=new HashMap<>();
		HashMap<String,Object> dataList=new HashMap<>();
		NumberFormat nt = NumberFormat.getPercentInstance();
		nt.setMinimumFractionDigits(2);//百分比保留小数点后两位数
		for(RowBase row:list){
			String dropping_rate="";
			Double droppingRate=0.0;
			HashMap<String,Object> data=new HashMap<>();
			if(row.getSafeString("NETWORK_ONLINE_TIME")!=null &&!"".equals(row.getSafeString("NETWORK_ONLINE_TIME"))){
				//获取掉线总时间
				droppingRate=Double.valueOf(row.getSafeString("NETWORK_ONLINE_TIME"));
				dropping_rate=nt.format((droppingRate)/(60*24*day));//获取在线总分钟
				data.put("DROPPING_RATE", dropping_rate);
			}else{
				data.put("DROPPING_RATE","0.00%");
			}
			//如果在线总时长大于时间
			if(droppingRate>(60*24*(day))){
				data.put("DROPPING_RATE","100%");
			}
			data.put("StatistisStationList", row);	
			//平均掉线时长
			if(row.getSafeInt("NETWORK_DROP_TIME_AVG")>0){
				data.put("NETWORK_DROP_TIME_AVG", toTime(row.getSafeInt("NETWORK_DROP_TIME_AVG")));
			}else{
				data.put("NETWORK_DROP_TIME_AVG", 0);
			}
			//总在线时长
			if(row.getSafeInt("NETWORK_ONLINE_TIME")>0){
				data.put("NETWORK_ONLINE_TIME", toTime(row.getSafeInt("NETWORK_ONLINE_TIME")));
			}else{
				data.put("NETWORK_ONLINE_TIME", 0);
			}
			
			resultlist.add(data);
		}	
		dataList.put("dataTotal", dataTotal);
		dataList.put("resultlist", resultlist);
		return dataList;
	}
	
	private Map<String,String> queryStationQueryParams() {
		Map<String,String> params = new HashMap<>();
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		String station_id = RequestUnit.getParameter("STATION_ID");
		String build_status = RequestUnit.getParameter("BUILD_STATUS");
		String station_name = RequestUnit.getParameter("STATION_NAME");
		String station_id_like = RequestUnit.getParameter("STATION_ID_LIKE");
		String companyCode = RequestUnit.getParameter("companyCode");
		String stationStatus = RequestUnit.getString("stationStatus");
		String otherStation = RequestUnit.getString("OTHER_STATION");
		String riverPart = RequestUnit.getString("riverPart");
		String riverCode = RequestUnit.getParameter("riverCodes");
		String station_order= RequestUnit.getParameter("station_order");
		//该站点类型包括污水处理厂的站点，主要是用于水质分析中通量部分。
		String waterType = RequestUnit.getParameter("waterType");
		//用于标识是湖泊还是水库
		String stationLocation = RequestUnit.getParameter("stationLocation");
		String[] waterTypes={};
		String types = "";
		if(waterType!=null) {
			waterTypes = waterType.split(",");
			for(int i=0;i<waterTypes.length;i++) {
				types += "'"+waterTypes[i]+"',";
			}
			types=types.substring(0, types.length()-1);
		}

		if (StringUtil.IsNotNullOrEmpty(riverPart)) {
			params.put("riverPart", riverPart);
		}
		if (StringUtil.IsNotNullOrEmpty(station_order)) {
			params.put("station_order", station_order);
		}
		if (StringUtil.IsNotNullOrEmpty(riverCode)) {
			String[] code=riverCode.split(",");
			String rivercode="";
			for(int i=0;i<code.length;i++) {
				rivercode+="'"+code[i]+"',";
			}
			rivercode=rivercode.substring(0, rivercode.length()-1);
			params.put("riverCode", rivercode);
		}
		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			params.put("wwtp_id", wwtp_id);
		}
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			params.put("flow_type", flow_type);
		}
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			params.put("water_type", water_type);
		}
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			params.put("station_id", station_id);
		}
		if (StringUtil.IsNotNullOrEmpty(build_status)) {
			params.put("build_status", build_status);
		}
		if(StringUtil.IsNotNullOrEmpty(station_name)){
			params.put("station_name", station_name);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id_like)){
			params.put("station_id_like", station_id_like);
		}
		if(StringUtil.IsNotNullOrEmpty(companyCode)){
			params.put("companyCode", companyCode);
		}
		if(StringUtil.IsNotNullOrEmpty(stationStatus)){
			params.put("stationStatus", stationStatus);
		}
		if(StringUtil.IsNotNullOrEmpty(otherStation)) {
			params.put("otherStation", otherStation);
		}
		if(StringUtil.IsNotNullOrEmpty(types)) {
			params.put("waterType", types);
		}
		if(StringUtil.IsNotNullOrEmpty(stationLocation)) {
			params.put("stationLocation", stationLocation);
		}

		return params;
	}
	/**
	 * 获取运营单位信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/queryOperationUnitList")
	public void queryOperationUnitList(HttpServletResponse res) throws Exception {
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.queryOperationUnitList(pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 新增站点信息
	 * 熊伟
	 * 2016年06月14日
	 * @param res
	 */
	@RequestMapping (value = "/createStation")
	public void Create(HttpServletResponse res){
		HashMap<String, Object> parms = RequestUnit.getHashMapParmO();
		String tableName = RequestUnit.getString("tableName");
		DataUtil.Create(tableName, parms);
		String jsonStr = "{\"data\":\"success\"}";
		outString(jsonStr);
	}
	/**
	 * 更新站点信息
	 * 2016年06月14日
	 * @param res
	 * @throws Exception 
	 */
	@RequestMapping (value = "/updateStation")
	public void updateCompany(HttpServletResponse res){
		try {
			stationInfoService.updateCompanyInfo();
			String jsonStr = "{\"data\":\"success\"}";
			outString(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据污水处理厂id获取运营单位信息
	 */
	@RequestMapping(value = "/getOperationUnitByWWTPIDList")
	public void getOperationUnitByWWTPID(HttpServletResponse res) {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getOperationUnitByWWTPIDList(wwtp_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}

	/**
	 * 获取数采仪信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/queryDataCollectionInstrumentList")
	public void queryDataCollectionInstrument(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_COLLECTION_INFO");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.queryDataCollectionInstrumentList(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}


	/**
	 * 获取在线监测设备信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/queryMonitorEquipmentList")
	public void queryMonitorEquipment(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("MONITOR_EQUIPMENT");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.queryMonitorEquipmentList(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}

	/**
	 * 获取站点污染物
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryStationPollutantList")
	public void queryStationPollutant(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		JSONObject json = stationInfoService.queryStationPollutantList();
		outString(json.toString());
	}
	
	/**
	 * 根据站点获取门禁数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getAccessDataById")
	public void getAccessDataById(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String WWTP_ID=RequestUnit.getParameter("WWTP_ID");
		int pageSize = Integer.parseInt(resq.getParameter("pageSizeData"));
		int pageIndex = Integer.parseInt(resq.getParameter("pageIndexData"));
		String sql=" select * from t_b_station where 1=1 ";
		if(StringUtil.IsNotNullOrEmpty(WWTP_ID)){
			sql+= " and WWTP_ID= '"+WWTP_ID+"'";
		}
		String json= DbManager.getInstance().getMsSqlExecuter().executeGridJson(sql, pageSize, pageIndex);
		outString(json);
	}
	
	/**
	 * 删除数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAccessData")
	public void deleteAccessData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String STATION_CODE=RequestUnit.getParameter("station_code");
		String sql="delete from t_b_station where STATION_CODE='"+STATION_CODE+"'";
		DbManager.getInstance().getMsSqlExecuter().executeUpdate(sql);
		
	}
	
	/**
	 * 保存信息
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/savaAccessData")
	public void savaAccessData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String STATION_CODE=RequestUnit.getParameter("station_code");
		int f_ControllerSN = Integer.parseInt(resq.getParameter("f_controllersn"));
		String STATION_NAME=RequestUnit.getParameter("STATION_NAME");
		String WWTP_ID=RequestUnit.getParameter("WWTPID");
		String sqlc="select * from t_b_station where STATION_CODE='"+STATION_CODE+"'";
		String json= DbManager.getInstance().getMsSqlExecuter().executeGridJson(sqlc, 10, 1);
		String sql="";
		if(json.contains("f_controllersn")){
			sql="update t_b_station set STATION_CODE='"+STATION_CODE+"',STATION_NAME='"+STATION_NAME+"',f_ControllerSN="+f_ControllerSN+" where STATION_CODE='"+STATION_CODE+"'";
		}
		else{
			 sql=" insert into  t_b_station(STATION_CODE,f_ControllerSN,STATION_NAME,f_ControllerName,WWTP_ID) values('"+STATION_CODE+"',"+f_ControllerSN+",'"+STATION_NAME+"','','"+WWTP_ID+"')";
		}
		
		DbManager.getInstance().getMsSqlExecuter().executeUpdate(sql);
	}
	
	/**
	 * 获取视频信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getVideoData")
	public void getVideoData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getVideoDataByWWTPID(wwtp_id,station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
		
	}
	
	/**
	 * 根据污水处理厂id获取运营单位信息
	 */
	@RequestMapping(value = "/getOperationUnitByStationID")
	public void getOperationUnitByStationID(HttpServletResponse res) {
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getOperationUnitByStationId(station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	// 获取污水处理厂所属监测站点
	@RequestMapping(value = "/station")
	public void station(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getStationQueryParams();
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("MONITOR_STATION");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.queryStation(params, pTable, null);
		Collections.sort(list, new StationOnlineCompare());
		String result = DataUtil.listToJSONObject(list, null, pageInfo).toString();
		outString(result);
	}
	/**
	 * 获取数采仪信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/getDataCollectionInstrument")
	public void getDataCollectionInstrument(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_COLLECTION_INFO");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getDataCollectionInstrument(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 获取在线监测设备信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/getMonitorEquipment")
	public void getMonitorEquipment(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("MONITOR_EQUIPMENT");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getMonitorEquipment(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 手动执行站点在线状态统计
	 * gislin
	 * 2016年8月12日
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value = "/StaticStationOnlineStatus")
	public void StaticStationOnlineStatus(HttpServletRequest resq, HttpServletResponse res){
		String date = RequestUnit.getParameter("date");
		String station_code = RequestUnit.getParameter("station_code");
		try {
			stationInfoService.OnlineStatusStaticTrigger(date, date,station_code);
		} catch (Exception e) {
			String exc = ExecptionUtil.getStackTrace(e);
			outString(exc);
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取DVR信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getDVRData")
	public void getDVRData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getDVRDataByWWTPID(wwtp_id,station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
		
	}
	/**
	 * 获取运营单位信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getOperationUnit")
	public void getOperationUnit(HttpServletResponse res) throws Exception {
		String guid = RequestUnit.getParameter("GUID");
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(guid)){
			params.put("GUID", guid);
		}
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = stationInfoService.getOperationUnit(params, null, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 获取设备站点名称和代码列表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStationCodeList")
	public void getStationCode(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wsclcid = RequestUnit.getString("WSCLC_ID");
		String station_id = RequestUnit.getString("stationCode");
		String flow_type = RequestUnit.getString("flowType");
		String station_category = RequestUnit.getString("stationType");
		List<GlobalParameter> params = new ArrayList<GlobalParameter>();
		
		String sql = "select distinct m.name,m.code,decode(k.status,null,'noaccept','1','acceptpass','2','acceptfail') as status from BC_SITE_INFO m left join"
				+ " (select * from (select d.wsclc_id,  d.station_code,  d.status,  d.accep_date,"
				+ " row_number() over(partition by d.wsclc_id, d.station_code order by d.accep_date desc) as rank"
				+ "  from BC_DEVICE_ACCE_INFO d) t where t.rank=1) k on m.wwtp_id=k.wsclc_id and m.code=k.station_code"
				+ "  where 1=1 ";
		
		if(StringUtil.IsNotNullOrEmpty(wsclcid)){
			sql+= " and m.wwtp_id=@WWTPID";
			params.add(new GlobalParameter("WWTPID", wsclcid));
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			sql += " and m.code=@station_id";
			params.add(new GlobalParameter("station_id", station_id));
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			sql += " and m.flow_type=@flow_type";
			params.add(new GlobalParameter("flow_type", flow_type));
		}
		if(StringUtil.IsNotNullOrEmpty(station_category)){
			sql += " and m.station_category=@station_category";
			params.add(new GlobalParameter("station_category", station_category));
		}
		
		String result = DataUtil.getSelectJson(sql, params, null);
		outString(result);
	}
	
	
	
	private Map<String,String> getStationQueryParams() {
		Map<String,String> params = new HashMap<>();
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		String station_id = RequestUnit.getParameter("STATION_ID");
		String build_status = RequestUnit.getParameter("BUILD_STATUS");
		String station_name = RequestUnit.getParameter("STATION_NAME");
		String station_id_like = RequestUnit.getParameter("STATION_ID_LIKE");
		String companyCode = RequestUnit.getParameter("companyCode");
		String stationStatus = RequestUnit.getString("stationStatus");

		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			params.put("wwtp_id", wwtp_id);
		}
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			params.put("flow_type", flow_type);
		}
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			params.put("water_type", water_type);
		}
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			params.put("station_id", station_id);
		}
		if (StringUtil.IsNotNullOrEmpty(build_status)) {
			params.put("build_status", build_status);
		}
		if(StringUtil.IsNotNullOrEmpty(station_name)){
			params.put("station_name", station_name);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id_like)){
			params.put("station_id_like", station_id_like);
		}
		if(StringUtil.IsNotNullOrEmpty(companyCode)){
			params.put("companyCode", companyCode);
		}
		if(StringUtil.IsNotNullOrEmpty(stationStatus)){
			params.put("stationStatus", stationStatus);
		}

		return params;
	}
}
